from pyecharts.charts import Bar, Pie, Scatter, Funnel, WordCloud
from pyecharts import options as opts


def movie_year_chart(args, max_count):
    chart = Bar()
    chart.add_xaxis(args[0])
    chart.add_yaxis(
        '电影数目',
        args[1],
        itemstyle_opts=opts.ItemStyleOpts(
            color="#91cc75",  # 使用柔和的绿色
            opacity=0.8,
            border_radius=[5, 5, 0, 0]  # 给柱状添加圆角
        )
    )
    chart.set_global_opts(
        title_opts=opts.TitleOpts(
            title="电影年份分布",
            subtitle="基于豆瓣电影数据",
            pos_left="center"
        ),
        legend_opts=opts.LegendOpts(is_show=False),
        tooltip_opts=opts.TooltipOpts(
            trigger="axis",
            axis_pointer_type="shadow"
        ),
        visualmap_opts=opts.VisualMapOpts(
            is_show=True,
            max_=max_count,
            range_color=["#d4edc4", "#41ae3c"]  # 从浅绿到深绿的渐变
        ),
        xaxis_opts=opts.AxisOpts(
            axislabel_opts=opts.LabelOpts(rotate=45),  # 旋转标签避免重叠
            name="年份",
            name_location="end"
        ),
        yaxis_opts=opts.AxisOpts(
            name="电影数量",
            name_location="end",
            name_gap=15
        ),
        datazoom_opts=[
            opts.DataZoomOpts(range_start=0, range_end=100),  # 添加缩放功能
            opts.DataZoomOpts(type_="inside")
        ]
    )
    return chart.dump_options_with_quotes()


def movie_type_chart(args):
    chart = Pie()
    chart.add(
        series_name="电影类型",
        data_pair=args,
        radius=["35%", "60%"],
        center=["50%", "50%"],
        rosetype="radius",  # 使用玫瑰图样式
        label_opts=opts.LabelOpts(
            position="outside",
            formatter="{b}: {d}%",
            font_size=12,
            font_style="normal",
            font_weight="bold"
        )
    )
    chart.set_global_opts(
        title_opts=opts.TitleOpts(
            title="电影类型分布",
            subtitle="基于豆瓣电影数据",
            pos_left="center",
            padding=[0, 0, 20, 0]
        ),
        tooltip_opts=opts.TooltipOpts(
            trigger="item",
            formatter="{a} <br/>{b}: {c} ({d}%)"
        ),
        legend_opts=opts.LegendOpts(
            type_="scroll",
            orient="vertical",
            pos_left="5%",
            pos_top="middle",
            item_width=25,
            item_height=14,
            align="left",
            page_button_position="end",
            page_icon_size=15,
            page_icon_color="#666666"
        )
    )
    chart.set_colors([
        "#5470c6", "#91cc75", "#fac858", "#ee6666",
        "#73c0de", "#3ba272", "#fc8452", "#9a60b4",
        "#ea7ccc", "#48b3bd"
    ])
    return chart.dump_options_with_quotes()


def movie_rating_chart(args):
    chart = Scatter()
    x_data = [item[0] for item in args]  # 电影名称
    y_data = [item[1] for item in args]  # 评分
    
    chart.add_xaxis(x_data)
    chart.add_yaxis(
        series_name="电影评分",
        y_axis=y_data,
        symbol_size=15,
        label_opts=opts.LabelOpts(is_show=False),
        itemstyle_opts=opts.ItemStyleOpts(
            color="#5470c6",
            opacity=0.7,
            border_color="#fff",
            border_width=1
        )
    )

    chart.set_global_opts(
        title_opts=opts.TitleOpts(
            title="电影评分分布",
            subtitle="基于豆瓣电影数据",
            pos_left="center"
        ),
        tooltip_opts=opts.TooltipOpts(
            formatter=lambda params: f"电影：{params.name}<br/>评分：{params.value[1]}"
        ),
        legend_opts=opts.LegendOpts(is_show=False),
        xaxis_opts=opts.AxisOpts(
            type_="category",
            name="电影名称",
            name_location="end",
            axislabel_opts=opts.LabelOpts(rotate=45, interval=0),
            splitline_opts=opts.SplitLineOpts(is_show=False)
        ),
        yaxis_opts=opts.AxisOpts(
            type_="value",
            name="评分",
            name_location="end",
            min_=8,
            max_=10,
            splitline_opts=opts.SplitLineOpts(is_show=True),
            axisline_opts=opts.AxisLineOpts(is_show=True),
            axistick_opts=opts.AxisTickOpts(is_show=True)
        ),
        datazoom_opts=[
            opts.DataZoomOpts(range_start=0, range_end=100),
            opts.DataZoomOpts(type_="inside")
        ]
    )
    return chart.dump_options_with_quotes()


def movie_top_chart(args):
    chart = Funnel()
    chart.add(
        series_name="电影TOP10",
        data_pair=args,
        gap=2,
        label_opts=opts.LabelOpts(
            position="inside",
            formatter="{b}",
            font_size=12,
            font_weight="bold",
            color="#fff"
        ),
        itemstyle_opts=opts.ItemStyleOpts(
            opacity=0.8,
            border_color="#fff",
            border_width=1
        ),
        tooltip_opts=opts.TooltipOpts(
            formatter="{a} <br/>{b}: 评分 {c}"
        ),
        min_=8,
        max_=10,
        sort_="descending",
        min_size='40%',
        max_size='85%'
    )
    chart.set_global_opts(
        title_opts=opts.TitleOpts(
            title="豆瓣电影TOP10",
            subtitle="基于评分排名",
            pos_left="center"
        ),
        legend_opts=opts.LegendOpts(is_show=False),
        tooltip_opts=opts.TooltipOpts(
            trigger="item",
            formatter="{a} <br/>{b}: 评分 {c}"
        )
    )
    chart.set_colors([
        "#5470c6", "#91cc75", "#fac858", "#ee6666",
        "#73c0de", "#3ba272", "#fc8452", "#9a60b4",
        "#ea7ccc", "#48b3bd"
    ])
    return chart.dump_options_with_quotes()


def movie_rater_count_chart(args):
    chart = Bar()
    chart.add_xaxis(args[0])
    chart.add_yaxis(
        '评分人数',
        args[1],
        label_opts=opts.LabelOpts(is_show=False),
        itemstyle_opts=opts.ItemStyleOpts(
            color="#5470c6",
            opacity=0.8,
            border_radius=[0, 5, 5, 0]  # 给柱状添加圆角
        )
    )
    chart.set_global_opts(
        title_opts=opts.TitleOpts(
            title="电影评分人数分布",
            subtitle="基于豆瓣电影数据",
            pos_left="center"
        ),
        legend_opts=opts.LegendOpts(is_show=False),
        tooltip_opts=opts.TooltipOpts(
            trigger="axis",
            axis_pointer_type="shadow",
            formatter="{b}: {c}人"
        ),
        xaxis_opts=opts.AxisOpts(
            name="电影名称",
            name_location="end",
            axislabel_opts=opts.LabelOpts(rotate=45)
        ),
        yaxis_opts=opts.AxisOpts(
            name="评分人数",
            name_location="end",
            name_gap=15,
            axislabel_opts=opts.LabelOpts(formatter="{value}")
        ),
        datazoom_opts=[
            opts.DataZoomOpts(range_start=0, range_end=100),
            opts.DataZoomOpts(type_="inside")
        ]
    )
    # 翻转x轴的方法
    chart.reversal_axis()
    return chart.dump_options_with_quotes()


def movie_words_cloud(args):
    chart = WordCloud()
    chart.add(
        series_name="电影名称",
        data_pair=args,
        word_size_range=[12, 60],  # 字体大小范围
        textstyle_opts=opts.TextStyleOpts(
            font_family="Microsoft YaHei"
        ),
        shape="circle"  # 设置词云形状为圆形
    )
    chart.set_global_opts(
        title_opts=opts.TitleOpts(
            title="电影名称词云",
            subtitle="基于豆瓣电影数据",
            pos_left="center"
        ),
        tooltip_opts=opts.TooltipOpts(
            trigger="item",
            formatter="{b}: {c}"
        )
    )
    return chart.dump_options_with_quotes()
